package com.lry.jvm.util;

import com.lry.jvm.rtda.Slot;

public class Util {
    public static int byteToInt(byte[] codes) {
        String s1 = byteToHexString(codes);
        return Integer.valueOf(s1, 16).intValue();
    }

    public static String byteToHexString(byte[] codes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < codes.length; i++) {
            byte b = codes[i];
            int value = b & 0xFF;
            String strHex = Integer.toHexString(value);
            if (strHex.length() < 2) {
                strHex = "0" + strHex;
            }
            sb.append(strHex);
        }
        return sb.toString();
    }

    public static void panic(String msg) {
        throw new RuntimeException(msg);
    }

    public static Slot setInt(int val) {
        Slot slot = new Slot(val);
        return slot;
    }

    public static int getInt(Slot slot) {
        return (int) slot.getNum();
    }

    public static Slot[] setLong(long val) {
        Slot[] slots = new Slot[2];
        slots[0]= new Slot( val);//低位
        slots[1]= new Slot(val>>32);//高位
        return slots;
    }
    public static long getLong(Slot[] slots) {
        long high = slots[1].getNum();
        long low = slots[0].getNum();
        return high<<32 | low;
    }


}
